package com.hpeng.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.basic.service.CategoryManager;
import com.hpeng.sys.model.Category;
import com.opensymphony.xwork2.ActionSupport;


@Component("downloadExcel")
@Scope("prototype")
public class DownloadExcel extends ActionSupport implements
		ServletResponseAware, ServletRequestAware {
	
	private String[][] rowDate;
	private String[] columnName;

	protected HttpServletRequest request = null;
	protected HttpServletResponse response = null;

	@Override
	public String execute() {
		if (rowDate.length != 0) {
			int page = 0;
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			WritableWorkbook book;
			try {
				//避免乱码
				WorkbookSettings setting = new WorkbookSettings();
				Locale local = new Locale("zh", "CN");
				setting.setLocale(local);
				setting.setEncoding("ISO-8859-1");
				book = Workbook.createWorkbook(os,setting);
				
				WritableSheet sheet = book.createSheet("第一页", page);//设置工作簿当前页数
				//设置表头
				for(int j=0; j<columnName.length; j++){
				//Label..第一个参数为列,第二个参数为行,第三个参数为值.
					Label label1 = new Label(j, 0, columnName[j]);
					sheet.addCell(label1);
				}
				//输出资产类型值
				for (int i = 0; i < rowDate.length; i++) {
					for(int j=0; j<rowDate[i].length; j++){
						Label label = new Label(j,i+1,rowDate[i][j]);
						sheet.addCell(label);
					}
				}
				book.write();
				book.close();
				request.setAttribute("excelStream", new ByteArrayInputStream(os
						.toByteArray()));
				request.setAttribute("filename", "myexcel.xls");//设置文件名 
				return "success";
			} catch (IOException e) {
				e.printStackTrace();
				return "error";
			} catch (RowsExceededException e) {
				e.printStackTrace();
				return "error";
			} catch (WriteException e) {
				e.printStackTrace();
				return "error";
			}
		}else{
			System.out.println("统计数据不存在");
			return "error";
		}
	}

	public void setServletResponse(HttpServletResponse resp) {
		this.response = resp;
	}

	public void setServletRequest(HttpServletRequest req) {
		this.request = req;
	}

	// 获取输出流
	public InputStream getInputExcel() {
		return (InputStream) request.getAttribute("excelStream");
	}

	public void setRowDate(String[][] rowDate) {
		this.rowDate = rowDate;
	}

	public void setColumnName(String[] columnName) {
		this.columnName = columnName;
	}
}
